<?php
    // estilos -- ver por que no puedo usar class con un css - otra clase debe estar bloqueandolo
    $tabla_orga_td = 'vertical-align: middle; background-color: transparent; border: 1px;';    
    // imagenes
    $prev_img = $this->Html->image("restaurante/ic_navegar_left.png", array("width" => "32px", "height" => "32px", "id" => "nav_icon_left"));    
    $next_img = $this->Html->image("restaurante/ic_navegar.png", array("width" => "32px", "height" => "32px", "id" => "nav_icon_right"));    
    $prev_img_disabled = $this->Html->image("restaurante/ic_nav_left_disabled.png", array("width" => "32px", "height" => "32px", "id" => "nav_icon_left_disabled"));    
    $next_img_disabled = $this->Html->image("restaurante/ic_navegar_disabled.png", array("width" => "32px", "height" => "32px", "id" => "nav_icon_right_disabled"));    


    $volver_img = $this->Html->image("restaurante/ic_back.png", array("id" => "back_icon"));    
    $nuevo_img = $this->Html->image("restaurante/ic_add.png", array("id" => "add_icon"));    
    $cancel_img = $this->Html->image("restaurante/ic_cancel.png", array("id" => "back_icon"));    
    $save_img = $this->Html->image("restaurante/ic_ok.png", array("id" => "save_icon"));    

    $pedido_facturar_img = $this->Html->image("restaurante/facturar_icon.png", array("width" => "48px", "height" => "48px"));    
    
    $editar_img = $this->Html->image("restaurante/doc_images.png", array("width" => "24px", "height" => "24px"));    
    $elim_img = $this->Html->image("restaurante/delete_small.png", array("width" => "12px", "height" => "12px"));    
    $undo_img = $this->Html->image("restaurante/undo_small.png", array("width" => "24px", "height" => "24px"));    
?>

<?php $options = array(1 => 'Efectivo', 2 => 'Tarjeta crédito', 3 => 'Tarjeta débito'); ?>    
<?php App::uses('CakeNumber', 'Utility'); ?>

<?php  // Debugger::dump($pedido['Cabecera']); ?>

<!--- MARCO DE LA PANTALLA ------>
<table><tr><td style="width: 20%; border: none;"></td>
        <td style="padding: 20px; background: white; border: none;">

<table>
    <tr>
        <td style="width: 15%; text-align:right; <?php echo $tabla_orga_td;?>"><div><?php echo $this->Html->image("restaurante/ic_resto.png", array("id" => "resto_icon"));?></div></td>
        <td style="width: 55%; text-align:left; <?php echo $tabla_orga_td;?>"><h1 class="titulo_ventana_2">Facturar pedido</h1></td>
        <td style="width: 30%; text-align:right; <?php echo $tabla_orga_td;?>">
            <table>
                <tr><td style="text-align:right; <?php echo $tabla_orga_td;?>"><?php echo 'Usuario: '.$nombre_usuario;?></td></tr>
                <tr><td style="text-align:right; <?php echo $tabla_orga_td;?>"><?php echo $this->Html->link('Cerrar sesión', array("controller"=>"users", "action"=>"logout"), array('title' => 'Cerrar sesión', 'escape' => false));?></td></tr>
            </table>
        </td>
    </tr>
</table>
<hr>

<?php echo $this->Form->create('Pedido', array('action'=>'pagar')); ?>
<?php //Debugger::dump($errores); ?>

<?php echo $this->Form->input('pedido_id', array( 'type' => 'hidden', 'value' => $pedido['Cabecera']['Pedido']['id'])); ?>

<!--- CONTROLES Y BOTONERA FILTROS ------>
<table style="<?php echo $tabla_orga_td;?>">
    <tr style="<?php echo $tabla_orga_td;?>">
        <td style="width: 20%; text-align:right;" class="categoria_label_required">
            <?php echo $this->Form->label('medio_pago', 'Medio de pago<span style="color: red; ">*</span>', array('class' => 'required')); ?></td>
        <td style="width: 30%; vertical-align: middle; <?php echo $tabla_orga_td;?>"><?php echo $this->Form->radio('medio_pago', $options, array('legend'=> false)); ?></td>
        <td style="width: 10%;<?php echo $tabla_orga_td;?>">
            <?php 

                if(isset($errores['importe'][0])) {
                    echo '<div class="input text required error" style="padding-right:10px;">';
                    echo $this->Form->input('importe', array( 'type' => 'number', 'label' => 'Importe($)', 'class' => 'form-error')); 
                    echo '<div class="error-message">'.$errores['importe'][0].'</div>';
                    echo '</div>';
                } else {
                    echo $this->Form->input('importe', array( 'type' => 'number', 'label' => 'Importe($)')); 
                }
                
            ?>
        </td>
        <td style="width: 10%; text-align:right; <?php echo $tabla_orga_td;?>"><?php echo 'Agregar'; ?></td>
        <td style="width: 10%; text-align:left; <?php echo $tabla_orga_td;?>"><?php echo $this->Form->submit('restaurante/ic_add.png',array('class' => 'boton-buscar')); ?> </td>
        <td style="width: 10%; text-align:right; <?php echo $tabla_orga_td;?>"><?php echo 'Limpiar'; ?></td>
        <td style="width: 10%; text-align:left; <?php echo $tabla_orga_td;?>"><?php echo $this->Form->button('', array('type'=>'reset','class' => 'boton-limpiar')); ?></td>    
    </tr>    
</table>
     
<!--- GRILLA DE MEDIOS DE PAGO ------>
<table style="width: 80%; padding-left: 20px;">
    <tr>
        <th style="width: 50%;">Medio de pago</th>
        <th style="width: 30%; text-align:right;">Importe($)</th>
        <th style="width: 20%; text-align:center;">Eliminar</th>
    </tr>
    <?php foreach ($pedido['Valor'] as $pago): ?>
    <tr>
        <td style="width: 50%;"><?php echo $options[$pago['PedidoValor']['medio_pago']]; ?></td>
        <td style="width: 30%; text-align: right;"><?php echo CakeNumber::precision($pago['PedidoValor']['importe'], 2); ?></td>
        <td style="width: 20%; text-align:center;"><?php echo $this->Html->link($elim_img, '/pedidovalors/anularpago/'.$pago['PedidoValor']['id'], array('escape' => false)); ?> </td>
    </tr>    
    <?php endforeach; ?>    
</table>
<br>

<!--- TOTAL MEDIOS DE PAGO ------>
<table style="width: 80%; padding-left: 20px;">
    <tr>
        <td style="width: 20%;<?php echo $tabla_orga_td;?>"></td>
        <td style="width: 40%;<?php echo $tabla_orga_td;?>"><?php echo $this->Form->label('lbl_medio_pago', 'Total medios de pago($)'); ?></td>
        <td style="width: 20%; text-align: right;<?php echo $tabla_orga_td;?>">
            <?php $total_medio_pago = $pedido['Cabecera']['Pedido']['impo_total_valores'];
            echo $this->Form->label('total_medio_pago', CakeNumber::precision($pedido['Cabecera']['Pedido']['impo_total_valores'], 2)); ?>
        </td>        
        <td style="width: 20%;<?php echo $tabla_orga_td;?>"></td>
    </tr>
    <tr>
        <td style="width: 20%;<?php echo $tabla_orga_td;?>"></td>
        <td style="width: 40%;<?php echo $tabla_orga_td;?>"></td>
        <td style="width: 20%;<?php echo $tabla_orga_td;?>"></td>
        <td style="width: 20%;<?php echo $tabla_orga_td;?>"></td>
    </tr>
<!--- IMPORTES FACTURA ------>    
    <tr>
        <td style="width: 20%;<?php echo $tabla_orga_td;?>"></td>
        <td style="width: 40%;<?php echo $tabla_orga_td;?>"><?php echo $this->Form->label('lbl_total_factura', 'Total factura($)'); ?></td>
        <td style="width: 20%; text-align: right;<?php echo $tabla_orga_td;?>">
            <?php             
            if ($pedido['Cabecera']['Pedido']['paga_cubierto'] == 1) {
                $impo_cubierto = $pedido['Cabecera']['Pedido']['precio_uni_cubierto'] * $pedido['Cabecera']['Pedido']['cant_clientes'];
            } else { $impo_cubierto = 0; }           
            $impo_total_sin_dto  = $pedido['Cabecera']['Pedido']['impo_total_detalles'] + $pedido['Cabecera']['Pedido']['impo_total_agregados'] + $impo_cubierto;
            if (isset($pedido['Cabecera']['Descuento']['porcentaje'])) {
                $impo_dto = round($impo_total_sin_dto * $pedido['Cabecera']['Descuento']['porcentaje'] / 100.0, 2);
            } else {
                $impo_dto = 0;
            }
            $total_factura = $impo_total_sin_dto - $impo_dto;
            echo $this->Form->label('total_factura', CakeNumber::precision($total_factura, 2)); ?>
        </td>        
        <td style="width: 20%;<?php echo $tabla_orga_td;?>"></td>
    </tr>
<!--- VUELTO ------>    
    <tr>
        <td style="width: 20%;<?php echo $tabla_orga_td;?>"></td>
        <td style="width: 40%;<?php echo $tabla_orga_td;?>"><?php echo $this->Form->label('lbl_vuelto', 'Vuelto($)'); ?></td>
        <td style="width: 20%; text-align: right;<?php echo $tabla_orga_td;?>">
            <?php 
                if ($total_medio_pago > $total_factura) $vuelto = $total_medio_pago - $total_factura; else $vuelto = 0;
                echo $this->Form->label('vuelto', CakeNumber::precision($vuelto, 2)); ?>
        </td>        
        <td style="width: 20%;<?php echo $tabla_orga_td;?>"></td>
    </tr>
</table>

<!--- BOTONERA ------>
<table>
    <tr>
        <td style="width: 10%; text-align:right; <?php echo $tabla_orga_td;?>"><?php echo 'Volver'; ?></td>
        <td style="width: 10%; text-align:left; <?php echo $tabla_orga_td;?>"><?php echo $this->Html->link($volver_img, '/pedidos/detalle/'.$pedido['Cabecera']['Pedido']['id'], array('class' => 'button', 'title' => 'Cancelar', 'escape' => false));?></td>
        <td style="width: 55%; <?php echo $tabla_orga_td;?>"></td>
        <td style="width: 15%; text-align:right; <?php echo $tabla_orga_td;?>"><?php echo 'Facturar'; ?></td>
        <td style="width: 10%; text-align:left; <?php echo $tabla_orga_td;?>"><?php echo $this->Html->link($pedido_facturar_img, '/pedidos/facturar/'.$pedido['Cabecera']['Pedido']['id'], array('class' => 'button', 'title' => 'Emitir ticket', 'escape' => false));?></td>
    </tr>
</table>

<?php echo $this->Form->end(); ?>

<!--- FIN MARCO DE LA PANTALLA ------>
</td><td style="width: 20%; border: none;"></tr></table>